package com.yx.cloud.activity;

import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.yx.cloud.R;
import com.yx.cloud.adapter.MusicTitleTabAdapter;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.bean.MusicTabBean;
import com.yx.cloud.databinding.MusicSearchResultListActivityBinding;
import com.yx.cloud.fragment.MusicListFragment;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.view.MusicViewPager;
import com.yx.cloud.view.SearchEditText;
import java.util.ArrayList;
import java.util.List;

public class MusicSearchResultListActivity extends BaseActivity implements View.OnClickListener, MusicTitleTabAdapter.InnerItemOnclickListener, SearchEditText.RightListener{
    private MusicSearchResultListActivityBinding binding;
    private MusicTitleTabAdapter musicTitleTabAdapter;
    private List<MusicTabBean> musicType;
    private List<MusicListFragment> fragmentList;
    private FragmentManager fm;
    private MusicViewPager musicViewPager;
    private RecyclerView recyclerView;
    private SearchEditText searchEditText;

    @Override
    public void onCreate() {
        binding = MusicSearchResultListActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initData();
        initView(binding.getRoot());
        initViewPager();
    }

    private void initView(View view){
        //设置线性布局管理器类型
        recyclerView = view.findViewById(R.id.rv_search_cate_tab_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        musicTitleTabAdapter = new MusicTitleTabAdapter(this, musicType);
        musicTitleTabAdapter.setOnInnerItemOnClickListener(this);
        recyclerView.setAdapter(musicTitleTabAdapter);
        musicViewPager = view.findViewById(R.id.vp_search_result_list);
        searchEditText = view.findViewById(R.id.et_search);
        searchEditText.setFocusable(true);
        searchEditText.setFocusableInTouchMode(true);
        searchEditText.requestFocus();
        //显示软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        searchEditText.setRightListener(this);
        binding.tvSearchMusicCancel.setOnClickListener(this);
        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                // TODO Auto-generated method stub
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    binding.llSearchList.setVisibility(View.VISIBLE);
                    if(fragmentList != null && fragmentList.size() > 0){
                        for(int i = 0; i < fragmentList.size();i++){
                            int finalI = i;
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    fragmentList.get(finalI).searchContent(v.getText().toString(),1,20,true);
                                }
                            }).start();
                        }
                    }
                    return true;
                }
                return false;
            }
        });
    }

    private void initData(){
        //TODO 获取音乐列表tab类型
        musicType = new ArrayList<>();
        MusicTabBean musicTabBean0 = new MusicTabBean();
        musicTabBean0.setCateId(MusicListFragment.CATE_TYPE_SEARCH_SONG);
        musicTabBean0.setMusicTopicName(getResources().getString(R.string.M_Single));
        musicType.add(musicTabBean0);
       /* MusicTabBean musicTabBean2 = new MusicTabBean();
        musicTabBean2.setCateId(MusicListFragment.CATE_TYPE_SEARCH_SINGER);
        musicTabBean2.setMusicTopicName(getResources().getString(R.string.music_singer));
        musicType.add(musicTabBean2);*/
        MusicTabBean musicTabBean3 = new MusicTabBean();
        musicTabBean3.setCateId(MusicListFragment.CATE_TYPE_SEARCH_ALBUM);
        musicTabBean3.setMusicTopicName(getResources().getString(R.string.M_Albums));
        musicType.add(musicTabBean3);
    }

    private void initViewPager() {
        fragmentList = new ArrayList<>();
        for(int i = 0;i < musicType.size();i++){
            MusicListFragment musicListFragment = new MusicListFragment(musicType.get(i).getCateId(),"ALBUM");
            fragmentList.add(musicListFragment);
        }

        fm = getSupportFragmentManager();
        MusicSearchResultListActivity.FragAdapter adapter = new MusicSearchResultListActivity.FragAdapter(fm, fragmentList);
        musicViewPager.setAdapter(adapter);
        musicViewPager.setOffscreenPageLimit(3);
        musicViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedTab(position);

                //横向recycleView中item居中显示
                int firstPosition = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0));
                int lastPosition = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(recyclerView.getChildCount() - 1));
                View view1 = recyclerView.getChildAt(position - firstPosition);
                View view2 = recyclerView.getChildAt(lastPosition - position);
                if(view1 == null || view2 == null){
                    return;
                }else {
                    int left = recyclerView.getChildAt(position - firstPosition).getLeft();
                    int right = recyclerView.getChildAt(lastPosition - position).getLeft();
                    recyclerView.scrollBy((left - right) / 2,0);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id== R.id.tv_search_music_cancel) {
            Intent_utils.backIntentFromTopQuickly(this);
        }
    }

    private void selectedTab(int position){
        musicTitleTabAdapter.selectItem(position);
    }

    @Override
    public void itemClick(View v, int position) {
        //TODO 选中tab高亮显示，并且切换相应音乐列表
        musicTitleTabAdapter.notifyDataSetChanged();
        musicViewPager.setCurrentItem(position);
    }

    @Override
    public void onDrawableRightClick(View view) {
        switch(view.getId()){
            case R.id.et_search:
                binding.etSearch.setText("");//点击右侧图片清空内容
                break;
        }

    }

    public class FragAdapter extends FragmentPagerAdapter {

        private List<MusicListFragment> mFragments;

        public FragAdapter(FragmentManager fm, List<MusicListFragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public MusicListFragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

    }
}
